package com.leedsoft.config.spring_config;

import com.google.common.collect.Sets;
import com.leedsoft.config.converters.DateConverter;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.extras.springsecurity4.dialect.SpringSecurityDialect;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.AbstractTemplateResolver;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;

import javax.servlet.ServletContext;
import java.nio.charset.Charset;
import java.util.List;

/**
 * spring mvc容器 相关配置
 */
@Configuration
@ComponentScan(basePackages = {"com.leedsoft.spittr.web"})
@EnableWebMvc
public class ServletConfig extends WebMvcConfigurerAdapter{
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();//静态资源处理器
    }

//    @Bean//javaConfig方式配置bean
//    public ViewResolver viewResolver() {//方法名为beanID
//        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
//        viewResolver.setPrefix("/WEB-INF/views/");
//        viewResolver.setSuffix(".jsp");
//        return viewResolver;//返回值为生成的bean
//    }

    @Bean
    public MultipartResolver multipartResolver() {
        return new StandardServletMultipartResolver();
    }

//    @Override//不可以覆盖configureMessageConverters，这样会让自动注册机制失效
//    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//        converters.add(new StringHttpMessageConverter(Charset.forName("utf-8")));
//        converters.add(new MappingJackson2HttpMessageConverter());//jackson-databind模块在classpath下，就会自动注册
//    }

    @Override//可以覆盖extendMessageConverters，调用add(0,XX)，将新加的消息转换器放到队列前面，从而覆盖自动注册，同时不影响其他自动注册的转换器
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(0, new StringHttpMessageConverter(Charset.forName("utf-8")));
    }

    @Override//注册类型转换converter。。注意，跟xml配置方式有较大差别。
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new DateConverter());
    }

    @Bean//message国际化
    public MessageSource messageSource() {
        ResourceBundleMessageSource ms = new ResourceBundleMessageSource();
        ms.setBasename("messages/message");//message文件前缀，包括目录路径
        ms.setDefaultEncoding("utf-8");//正确显示中文
        return ms;
    }
    //Thymeleaf
    @Bean//模板解析器，用来加载模板
    public AbstractTemplateResolver templateResolver(ServletContext servletContext) {//springmvc上下文，存在ServletContext bean
        ServletContextTemplateResolver tr = new ServletContextTemplateResolver(servletContext);
        tr.setPrefix("/WEB-INF/thymeleaf_tpls/");
        tr.setSuffix(".html");
        tr.setTemplateMode(TemplateMode.HTML);
        tr.setCharacterEncoding("utf-8");//加载模板的时候，就要指定utf-8。否则默认是ISO8859-1
        return tr;
    }
    @Bean//模板引擎----配置解析器
    public TemplateEngine templateEngine(AbstractTemplateResolver templateResolver) {
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(templateResolver);
//        templateEngine.setDialect(new SpringSecurityDialect());//不能这样添加Security方言，会覆盖默认方言
        templateEngine.setAdditionalDialects(Sets.newHashSet(new SpringSecurityDialect()));//需要添加额外方言
        return templateEngine;
    }
    @Bean//Thymeleaf 视图解析器----配置引擎
    public ViewResolver thymeleafViewResolver(TemplateEngine templateEngine) {
        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
        viewResolver.setTemplateEngine(templateEngine);
        viewResolver.setContentType("text/html;charset=UTF-8");//这个ContentType是设置response body的MIME
//        viewResolver.setOrder(0);
// Internal、Thymeleaf等视图解析器，一定会返回视图，如果找不到，则报404.所以，无法使用jsp和Thymeleaf共存
        return viewResolver;
    }

}
